package com.yswee.framework.widget.chart;

import java.util.Random;

import com.yswee.framework.R;
import com.yswee.framework.utils.UnitUtils;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;


public class BezierWaveView extends View {

    private Paint mPaint;
    private Path mPath;

    // view宽度
    private int width;
    // view高度
    private int height;
    // 波浪高低偏移量
    private int waveHeight = 50;
    // X轴，初始化+的偏移量
    private int startoffset = 0;
    // X轴，view的偏移量
    private int xoffset = 0;
    // view的Y轴高度
    private int viewY = 0;
    // 波浪速度
    private int waveSpeed = 60;
    // 波浪颜色
    private int waveColor = Color.RED;
    // 波浪线粗细
    private int waveStrokeWidth = 1;

    private ValueAnimator animator;

    public BezierWaveView(Context context) {
        this(context, null);
    }

    public BezierWaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initializeAttr(attrs);
        initializeView(context);
    }
	
	protected void initializeAttr(AttributeSet attrs) {
		TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
				R.styleable.BezierWaveView);

		waveSpeed = (int) typedArray.getFloat(R.styleable.BezierWaveView_BezierWaveView_wavespeed, waveSpeed);
		waveHeight = (int) typedArray.getDimension(R.styleable.BezierWaveView_BezierWaveView_waveheight, 
				UnitUtils.dip2pix(getContext(), waveHeight));
		waveColor = typedArray.getColor(R.styleable.BezierWaveView_BezierWaveView_wavecolor, waveColor); 
		waveStrokeWidth = (int) typedArray.getDimension(R.styleable.BezierWaveView_BezierWaveView_wavestrokewidth, 
				UnitUtils.dip2pix(getContext(), waveStrokeWidth));
		startoffset = (int) typedArray.getFloat(R.styleable.BezierWaveView_BezierWaveView_startoffset, startoffset);
		
		typedArray.recycle();
	}

    @SuppressWarnings("deprecation")
	private void initializeView(Context context) {

        mPaint = new Paint();
        mPaint.setColor(waveColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(waveStrokeWidth);

        mPath = new Path();

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        width = wm.getDefaultDisplay().getWidth();

        if (startoffset == 0) {
        	Random random = new Random();
        	startoffset = random.nextInt(width);
        }
        
        animator = new ValueAnimator();
        animator.setFloatValues(0, width);
        animator.setDuration(waveSpeed * 20);
        animator.setRepeatCount(-1);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float change = Float.parseFloat(animation.getAnimatedValue().toString());
                xoffset = (int) change + startoffset;
                Log.e("mlj", xoffset + "");
                invalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        //设置一个默认值，就是这个View的默认宽度为500，这个看我们自定义View的要求
        int result = 500;
        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值
            result = specSize;
        }
        width = result;
        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 500;
        if (specMode == MeasureSpec.AT_MOST) {
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        height = specSize;
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();
        viewY = height / 2;
        // 绘制屏幕内的波浪
        int x = xoffset;
        while (x >= width) {
        	x = x - width;
        }
        mPath.moveTo(x, viewY);
        mPath.quadTo(width / 4 + x, viewY - waveHeight, width / 2 + x, viewY);
        mPath.moveTo(width / 2 + x, viewY);
        mPath.quadTo(width / 4 * 3 + x, viewY + waveHeight, width + x, viewY);
        // 绘制屏幕外的波浪
        while (x > 0) {
	        x = x - width;
	        mPath.moveTo(x, viewY);
	        mPath.quadTo(width / 4 + x, viewY - waveHeight, width / 2 + x, viewY);
	        mPath.moveTo(width / 2 + x, viewY);
	        mPath.quadTo(width / 4 * 3 + x, viewY + waveHeight, width + x, viewY);
        }
        canvas.drawPath(mPath, mPaint);
    }
    
    public void start() {
    	animator.start();
    }
    
    public void stop() {
    	animator.end();
    }

}